/*
 * employeeTab.java
 *
 * Created on 12 novembre 2008, 10:13
 */

package GUI;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  coco
 */
 class CustomEditableCellTableModel extends DefaultTableModel {

        private ArrayList listCellNotEditable;

        public CustomEditableCellTableModel() {
            super();
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(int numRows, int numColumns) {
            super(numRows, numColumns);
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(Vector columnNames, int numRows) {
            super(columnNames, numRows);
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(Object[] columnNames, int numRows) {
            super(columnNames, numRows);
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(Vector data, Vector columnNames) {
            super(data, columnNames);
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(Object[][] data, Object[] columnNames) {
            super(data, columnNames);
            this.listCellNotEditable = new ArrayList();
        }

        public void addNotEditableCell(int row, int column) {
            if ( ! (column >= 0 && column < this.getColumnCount()) ) {
                return;
            }
            if ( ! ((row >= 0) && (row < this.getRowCount())) ) {
                return;
            }
            this.listCellNotEditable.add(new Point(row, column));
        }

        public void removeNotEditableCell(int row, int column) {
            int index = this.listCellNotEditable.indexOf(new Point(row, column));
            if ( index != -1 ) {
                this.listCellNotEditable.remove(index);
            }
        }
       public boolean isCellEditable(int row, int column) {
            int index = this.listCellNotEditable.indexOf(new Point(row, column));
            if ( index == -1 ) {
                return ( false );
            } else {
                return ( true );
            }
        }
    }
public class employeeTab extends javax.swing.JPanel {

    public static JTable centrerTable(JTable table) {
        DefaultTableCellRenderer custom = new DefaultTableCellRenderer();//on crée un nouveau model de DefaultTableCellRenderer
        custom.setHorizontalAlignment(JLabel.CENTER); //dans celle ci on centre les header

        //on centre pour chaque cellule
        for (int i=0 ; i<table.getColumnCount() ; i++)
            table.getColumnModel().getColumn(i).setCellRenderer(custom);
        return table;
    }
    /** Creates new form employeeTab */
    public employeeTab() throws ParseException {
        initComponents();
        String données[][];
                 if(GUI.LoginWindow.CanManageUsers)
         {
                 addemployeebutton.setEnabled(true);
                 }
                 else{
            addemployeebutton.setEnabled(false);
                 }
                données=kernel.Employee.searchemployee(GUI.LoginWindow.sessionID,1,"%%");

        //on affiche la jTable des utilisateurs'
        String[] nomsColonne={"Id","Name","Vorname","Firmeneintritt","Firmenaustritt","Benutzername","Passwort","Aktiv","Priviliegien"};
        CustomEditableCellTableModel jTable2 =new CustomEditableCellTableModel(données,nomsColonne) ;
        employeeTable.setModel(jTable2);
        employeeTable=centrerTable(employeeTable);
        employeeTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        employeeTable.setAutoCreateRowSorter(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        employeeTab = new javax.swing.JPanel();
        employeeNameLabel = new javax.swing.JLabel();
        employeeNameField = new javax.swing.JTextField();
        employeeIdName = new javax.swing.JLabel();
        employeeTableScroll = new javax.swing.JScrollPane();
        employeeTable = new javax.swing.JTable();
        employeeSearchButton = new javax.swing.JButton();
        addemployeebutton = new javax.swing.JButton();
        employeeIdField = new javax.swing.JFormattedTextField();

        employeeNameLabel.setText("Name");

        employeeNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                employeeNameFieldActionPerformed(evt);
            }
        });
        employeeNameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                employeeNameFieldKeyPressed(evt);
            }
        });

        employeeIdName.setText("Mitarbeiternummer");

        employeeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Id", "Name", "Vorname", "Firmeneintritt", "Firmenaustritt", "Benutzername", "Passwort", "Aktiv", "Privilegien"
            }
        ));
        employeeTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                employeeTableMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                employeeTableMouseEntered(evt);
            }
        });
        employeeTableScroll.setViewportView(employeeTable);
        employeeTable.getColumnModel().getColumn(0).setPreferredWidth(20);

        employeeSearchButton.setText("Suchen");
        employeeSearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                employeeSearchButtonActionPerformed(evt);
            }
        });
        employeeSearchButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                employeeSearchButtonKeyPressed(evt);
            }
        });

        addemployeebutton.setText("hinzufügen");
        addemployeebutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addemployeebuttonActionPerformed(evt);
            }
        });

        employeeIdField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        employeeIdField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                employeeIdFieldActionPerformed(evt);
            }
        });
        employeeIdField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                employeeIdFieldKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout employeeTabLayout = new javax.swing.GroupLayout(employeeTab);
        employeeTab.setLayout(employeeTabLayout);
        employeeTabLayout.setHorizontalGroup(
            employeeTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(employeeTabLayout.createSequentialGroup()
                .addGroup(employeeTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(employeeTabLayout.createSequentialGroup()
                        .addGap(385, 385, 385)
                        .addComponent(employeeNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(employeeNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(employeeIdName, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(employeeIdField, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(employeeSearchButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 408, Short.MAX_VALUE)
                        .addComponent(addemployeebutton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, employeeTabLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(employeeTableScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 1324, Short.MAX_VALUE)))
                .addContainerGap())
        );
        employeeTabLayout.setVerticalGroup(
            employeeTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(employeeTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(employeeTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(employeeNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(employeeNameLabel)
                    .addComponent(employeeIdName)
                    .addComponent(employeeSearchButton)
                    .addComponent(addemployeebutton)
                    .addComponent(employeeIdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(employeeTableScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(316, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1348, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, 0)
                    .addComponent(employeeTab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(0, 0, 0)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 538, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, 0)
                    .addComponent(employeeTab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(0, 0, 0)))
        );
    }// </editor-fold>//GEN-END:initComponents

private void employeeTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_employeeTableMouseClicked
if(evt.getClickCount()>1){
         if(GUI.LoginWindow.CanManageUsers)
         {
                try {
                    this.setEnabled(false);
                    employeeChange Frameemployee = new employeeChange();
                    employeeChange.addupdate = 2;
                    String[][] données;
                    int Rowselect = employeeTable.getSelectedRow();
                    Object id = employeeTable.getValueAt(Rowselect, 0);
                    données = kernel.Employee.searchemployee(GUI.LoginWindow.sessionID, Integer.parseInt(id.toString()), "3");
                    Frameemployee.setTitle("ändern eine Mitarbeiter");
                    Frameemployee.MitarbeiterNummer.setVisible(true);
                    Frameemployee.MitarbeiterNummer1.setVisible(true);
                    Frameemployee.MitarbeiterNummer1.setText(données[0][0]);
                    Frameemployee.AnmeldedatumMitarbeiter1.setText("");
                    Frameemployee.BenutzerNameMitarbeiter1.setText(données[0][5]);
                    Frameemployee.BeschaftigungsendeMitarbeiter1.setText(données[0][4]);
                    Frameemployee.FirmeneintrittMitarbeiter1.setText(données[0][3]);
                    Frameemployee.VornameMitarbeiter1.setText(données[0][2]);
                    Frameemployee.PasswortMitarbeiter1.setText(données[0][6]);
                    Frameemployee.NachnameMitarbeiter1.setText(données[0][1]);
                    Frameemployee.ActiveMitarbeiter1.setSelectedItem(données[0][7]);
                    if(données[0][8].contains("C")) Frameemployee.canmanagecar.setSelected(true);
                    else Frameemployee.canmanagecar.setSelected(false);
                    if(données[0][8].contains("M")) Frameemployee.canmanageuser.setSelected(true);
                    else Frameemployee.canmanageuser.setSelected(false);
                    if(données[0][8].contains("H")) Frameemployee.canseehistorie.setSelected(true);
                    else Frameemployee.canseehistorie.setSelected(false);
                    if(données[0][8].contains("S")) Frameemployee.canseestatistik.setSelected(true);
                    else Frameemployee.canseestatistik.setSelected(false);

                    Frameemployee.setSize(620, 350);
                    Frameemployee.setLocationRelativeTo(null);
                    Frameemployee.setVisible(true);
                } catch (ParseException ex) {
                    Logger.getLogger(employeeTab.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
}
}//GEN-LAST:event_employeeTableMouseClicked

private void employeeTableMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_employeeTableMouseEntered
// TODO add your handling code here:
}//GEN-LAST:event_employeeTableMouseEntered

private void employeeSearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_employeeSearchButtonActionPerformed
        try {
            String[][] données;
            int kundennumer;
            if (employeeIdField.getText().equals("")) {
                kundennumer = 0;
            } else {
                kundennumer = Integer.parseInt(employeeIdField.getText());
            }
            données = kernel.Employee.searchemployee(GUI.LoginWindow.sessionID, kundennumer, employeeNameField.getText());

            //on affiche la jTable des utilisateurs'
            String[] nomsColonne = {"Kundennummer", "Name", "Vorname", "Firmeneintritt", "Firmenaustritt", "Benutzername", "Passwort", "Aktiv", "Priviliegien"};
            CustomEditableCellTableModel jTable2 = new CustomEditableCellTableModel(données, nomsColonne);
            employeeTable.setModel(jTable2);
            employeeTable = centrerTable(employeeTable);
            employeeTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            employeeTable.setAutoCreateRowSorter(true);
        } catch (ParseException ex) {//GEN-LAST:event_employeeSearchButtonActionPerformed
            Logger.getLogger(employeeTab.class.getName()).log(Level.SEVERE, null, ex);
        }
}                                                    

private void addemployeebuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addemployeebuttonActionPerformed
           employeeChange Frameemployee=new employeeChange();
           employeeChange.addupdate=1;
           Frameemployee.setTitle("Hinzufügen einen Mitarbeiter");
           Frameemployee.MitarbeiterNummer.setVisible(false);
           Frameemployee.MitarbeiterNummer1.setVisible(false);
           String Datum=new Date().toString();
           String year=Datum.substring(8,10);
           String FinalDatum=Datum.substring(0,8);
           Frameemployee.AnmeldedatumMitarbeiter1.setText(FinalDatum+year);
           Frameemployee.BenutzerNameMitarbeiter1.setText("");
           Frameemployee.BeschaftigungsendeMitarbeiter1.setText("");
           Frameemployee.FirmeneintrittMitarbeiter1.setText("0001-01-01");
           Frameemployee.VornameMitarbeiter1.setText("");
           Frameemployee.PasswortMitarbeiter1.setText("");
           Frameemployee.NachnameMitarbeiter1.setText("");
           Frameemployee.ActiveMitarbeiter1.setSelectedIndex(0);
           Frameemployee.canmanagecar.setSelected(false);
           Frameemployee.canmanageuser.setSelected(false);
           Frameemployee.canseehistorie.setSelected(false);
           Frameemployee.canseestatistik.setSelected(false);

           Frameemployee.setSize(620, 350);
           Frameemployee.setLocationRelativeTo(null);
           Frameemployee.setVisible(true);

}//GEN-LAST:event_addemployeebuttonActionPerformed

private void employeeIdFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_employeeIdFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_employeeIdFieldActionPerformed

private void employeeNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_employeeNameFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_employeeNameFieldActionPerformed

private void employeeNameFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_employeeNameFieldKeyPressed
    if (evt.getKeyCode()==KeyEvent.VK_ENTER){
        String données[][] = null;
        int kundennumer;
        if(employeeIdField.getText().equals("")){
            kundennumer=0;
        }
        else
        kundennumer=Integer.parseInt(employeeIdField.getText());
        try {
            données = kernel.Employee.searchemployee(GUI.LoginWindow.sessionID, kundennumer, employeeNameField.getText());
        } catch (ParseException ex) {
            Logger.getLogger(employeeTab.class.getName()).log(Level.SEVERE, null, ex);
        }

        //on affiche la jTable des utilisateurs'
        String[] nomsColonne={"Kundennummer","Name","Vorname","Firmeneintritt","Firmenaustritt","Benutzername","Passwort","Aktiv","Priviliegien"};
        CustomEditableCellTableModel jTable2 =new CustomEditableCellTableModel(données,nomsColonne) ;
        employeeTable.setModel(jTable2);
        employeeTable=centrerTable(employeeTable);
        employeeTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        employeeTable.setAutoCreateRowSorter(true);
    }    // TODO add your handling code here:
}//GEN-LAST:event_employeeNameFieldKeyPressed

private void employeeIdFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_employeeIdFieldKeyPressed
    if (evt.getKeyCode()==KeyEvent.VK_ENTER){
        String données[][] = null;
        int kundennumer;
        if(employeeIdField.getText().equals("")){
            kundennumer=0;
        }
        else
        kundennumer=Integer.parseInt(employeeIdField.getText());
        try {
            données = kernel.Employee.searchemployee(GUI.LoginWindow.sessionID, kundennumer, employeeNameField.getText());
        } catch (ParseException ex) {
            Logger.getLogger(employeeTab.class.getName()).log(Level.SEVERE, null, ex);
        }

        //on affiche la jTable des utilisateurs'
        String[] nomsColonne={"Kundennummer","Name","Vorname","Firmeneintritt","Firmenaustritt","Benutzername","Passwort","Aktiv","Priviliegien"};
        CustomEditableCellTableModel jTable2 =new CustomEditableCellTableModel(données,nomsColonne) ;
        employeeTable.setModel(jTable2);
        employeeTable=centrerTable(employeeTable);
        employeeTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        employeeTable.setAutoCreateRowSorter(true);
    }     // TODO add your handling code here:
}//GEN-LAST:event_employeeIdFieldKeyPressed

private void employeeSearchButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_employeeSearchButtonKeyPressed
    if (evt.getKeyCode()==KeyEvent.VK_ENTER){
        String données[][] = null;
        int kundennumer;
        if(employeeIdField.getText().equals("")){
            kundennumer=0;
        }
        else
        kundennumer=Integer.parseInt(employeeIdField.getText());
        try {
            données = kernel.Employee.searchemployee(GUI.LoginWindow.sessionID, kundennumer, employeeNameField.getText());
        } catch (ParseException ex) {
            Logger.getLogger(employeeTab.class.getName()).log(Level.SEVERE, null, ex);
        }

        //on affiche la jTable des utilisateurs'
        String[] nomsColonne={"Kundennummer","Name","Vorname","Firmeneintritt","Firmenaustritt","Benutzername","Passwort","Aktiv","Priviliegien"};
        CustomEditableCellTableModel jTable2 =new CustomEditableCellTableModel(données,nomsColonne) ;
        employeeTable.setModel(jTable2);
        employeeTable=centrerTable(employeeTable);
        employeeTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        employeeTable.setAutoCreateRowSorter(true);
    } 
}//GEN-LAST:event_employeeSearchButtonKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addemployeebutton;
    private javax.swing.JFormattedTextField employeeIdField;
    private javax.swing.JLabel employeeIdName;
    private javax.swing.JTextField employeeNameField;
    private javax.swing.JLabel employeeNameLabel;
    private javax.swing.JButton employeeSearchButton;
    private javax.swing.JPanel employeeTab;
    public static javax.swing.JTable employeeTable;
    private javax.swing.JScrollPane employeeTableScroll;
    // End of variables declaration//GEN-END:variables

}
